<?php
/*
	[51parter.com] (C) 2009 Createindex Inc.
	$Id: admincp_help.php 20:54 2009-4-4 laozhang $
	@功能: 帮组中心
*/

if(!defined('IN_SITE') || !defined('IN_ADMINCP')) {
	exit('Access Denied');
}

//权限
if(!checkperm('managehelp')) {
	cpmessage('no_authority_management_operation');
	
}
//删除帮组
if(submitcheck('deletesubmit')) {
	include_once(S_ROOT.'./source/function_delete.php');
	if(!empty($_POST['ids']) && deletehelp($_POST['ids'])) {
		cpmessage('do_success', $_POST['mpurl']);
	} else {
		cpmessage('the_correct_choice_to_delete_the_help', $_POST['mpurl']);
	}
} elseif(submitcheck('helpsubmit')) {//添加或者编辑提交
	$setarr = array(
		'subject' => getstr(trim($_POST['subject']), 80, 1, 1, 1),
		'tag' => shtmlspecialchars(trim($_POST['tag'])),
		'message' => $_POST['message'],
		'classid' => intval($_POST['classid']),
	);

	if(!$setarr['classid'] || empty($setarr['subject']) || empty($setarr['message'])) {
		cpmessage('no_subject_and_message');
	}
	$_POST['helpid'] = intval($_POST['helpid']);
	if(empty($_POST['helpid'])) {
		$setarr['dateline'] = $_SGLOBAL['timestamp'];
		$setarr['uid'] =$_SGLOBAL['supe_uid'];
		inserttable('help', $setarr);
	} else {
		updatetable('help', $setarr, array('helpid'=>$_POST['helpid']));
	}
	
	
	cpmessage('do_success', 'admincp.php?ac=help');
	
	
}

$mpurl = 'admincp.php?ac=help';

//分类
$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('newsclass')." WHERE typeid=1 ORDER BY displayorder");
while ($value = $_SGLOBAL['db']->fetch_array($query)) {
	$classarr[$value['classid']] =$value;
}

if(empty($_GET['op'])) {
	//列表

	//处理搜索
	$intkeys = array('uid', 'helpid','classid');
	$strkeys = array();
	$randkeys = array(array('sstrtotime','dateline'), array('intval','viewnum'));
	$likekeys = array('subject','message');
	$results = getwheres($intkeys, $strkeys, $randkeys, $likekeys, 'b.');
	$wherearr = $results['wherearr'];
	$mpurl .= '&'.implode('&', $results['urls']);


	$wheresql = empty($wherearr)?'1':implode(' AND ', $wherearr);

	//排序
	$orders = getorders(array('dateline', 'viewnum'), 'helpid DESC', 'b.');
	$ordersql = $orders['sql'];
	if($orders['urls']) $mpurl .= '&'.implode('&', $orders['urls']);
	$orderby = array($_GET['orderby']=>' selected');
	$ordersc = array($_GET['ordersc']=>' selected');
	$classcheck[$_GET['classid']] = 'selected';

	$perpage = empty($_GET['perpage'])?0:intval($_GET['perpage']);
	if(!in_array($perpage, array(20,50,100,1000))) $perpage = 20;

	$page = empty($_GET['page'])?1:intval($_GET['page']);
	if($page<1) $page = 1;
	$start = ($page-1)*$perpage;
	//检查开始数
	ckstart($start, $perpage);

	//显示分页
	if($perpage > 100) {
		$count = 1;
		$selectsql = 'b.helpid';
	} else {
		$csql = "SELECT COUNT(*) FROM ".tname('help')." b WHERE $wheresql";
		$count = $_SGLOBAL['db']->result($_SGLOBAL['db']->query($csql), 0);
		$selectsql = '*';
	}
	$mpurl .= '&perpage='.$perpage;
	$perpages = array($perpage => ' selected');

	$list = array();
	$multi = '';

	$qsql = "SELECT $selectsql FROM ".tname('help')." b WHERE $wheresql $ordersql LIMIT $start,$perpage";

	if($count) {
		$query = $_SGLOBAL['db']->query($qsql);
		while ($value = $_SGLOBAL['db']->fetch_array($query)) {
			$list[$value['helpid']] = $value;
			$list[$value['helpid']]['classname'] = $classarr[$value['classid']]['classname'];
		}
		$multi = multi($count, $perpage, $page, $mpurl);
	}
	//显示分页
	if($perpage > 100) {
		$count = count($list);
	}
} elseif($_GET['op'] == 'add') {
	//添加
	
	$fck = fckeditor('message','<p></p>');

} elseif($_GET['op'] == 'edit') {
	//编辑
	$_GET['helpid'] = intval($_GET['helpid']);
	$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('help')." WHERE helpid='$_GET[helpid]' ");
	while ($value = $_SGLOBAL['db']->fetch_array($query)) {
		$help =$value;
	}
	
	$fck = fckeditor('message',$help['message']);
}
?>